package com.rxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.rxt.common.exception.CustomException;
import com.rxt.mapper.PProductMapper;
import com.rxt.mapper.PProductSkuMapper;
import com.rxt.model.domain.OShoppingCart;
import com.rxt.mapper.OShoppingCartMapper;
import com.rxt.model.domain.PProduct;
import com.rxt.model.domain.PProductSku;
import com.rxt.model.dto.UUserDto;
import com.rxt.model.vo.CartVo;
import com.rxt.model.vo.ProdNumVo;
import com.rxt.service.IOShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxt.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周梓文
 * @since 2023-08-24
 */
@Service
public class OShoppingCartServiceImpl extends ServiceImpl<OShoppingCartMapper, OShoppingCart> implements IOShoppingCartService {

    @Autowired
    private OShoppingCartMapper shoppingCartMapper;

    @Autowired
    private PProductSkuMapper productSkuMapper;

    @Autowired
    private PProductMapper productMapper;

    @Override
    public void addCart(CartVo cartVo) {
        UUserDto userDto = BaseContext.get();
        PProduct product = productMapper.selectById(cartVo.getProductId());//查出商品
        PProductSku productSku = productSkuMapper.selectById(cartVo.getSkuId());//查出规格
        QueryWrapper<OShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userDto.getId())
                .eq("sku_id",productSku.getId());
        OShoppingCart shoppingCart = shoppingCartMapper.selectOne(wrapper);
        if (shoppingCart != null){
            int number = shoppingCart.getNumber();
            number++;
            shoppingCart.setNumber(number);
            BigDecimal amount = productSku.getPrice().multiply(BigDecimal.valueOf(shoppingCart.getNumber()));
            shoppingCart.setAmount(amount);
            shoppingCartMapper.updateById(shoppingCart);
        }else {
            OShoppingCart cart = new OShoppingCart();
            cart.setUserId(userDto.getId());
            cart.setProductId(cartVo.getProductId());
            cart.setProductName(product.getTitle());
            cart.setImage(product.getPic());
            cart.setSkuId(cartVo.getSkuId());
            cart.setProductSku(productSku.getSku());
            cart.setPrice(productSku.getPrice());
            cart.setNumber(1);
            BigDecimal amount = productSku.getPrice().multiply(BigDecimal.valueOf(cart.getNumber()));
            cart.setAmount(amount);
            shoppingCartMapper.insert(cart);
        }
    }

    @Override
    public List<OShoppingCart> findCart() {
        Long userId = BaseContext.get().getId();
        QueryWrapper<OShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return shoppingCartMapper.selectList(wrapper);
    }

    @Override
    public void deleteBatch(List<Long> cartIds) {
        UUserDto userDto = BaseContext.get();
        for (Long cartId : cartIds) {
            LambdaQueryWrapper<OShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OShoppingCart::getUserId,userDto.getId())
                    .eq(OShoppingCart::getId,cartId);
            shoppingCartMapper.delete(wrapper);
        }
    }

    @Override
    public void updateNumber(ProdNumVo prodNumVo) {
        OShoppingCart shoppingCart = shoppingCartMapper.selectById(prodNumVo.getId());
        LambdaUpdateWrapper<OShoppingCart> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OShoppingCart::getId,prodNumVo.getId())
                .set(OShoppingCart::getNumber,prodNumVo.getNum())
                .set(OShoppingCart::getAmount,shoppingCart.getPrice().multiply(BigDecimal.valueOf(prodNumVo.getNum())));
        this.update(null,wrapper);
    }
}
